//
//  UIView+FrameChange.m
//  WZB
//
//  Created by WZB on 15-7-13.
//  Copyright (c) 2015年 WZB. All rights reserved.
//

#import "UIView+FrameChange.h"

@implementation UIView (FrameChange)
- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin
{
    return self.frame.origin;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}
- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (CGFloat)right {
    return self.x + self.width;
}

- (void)setRight:(CGFloat)right {
    self.x = right - self.width;
}

- (CGFloat)bottom {
    return self.y + self.height;
}

- (void)setBottom:(CGFloat)bottom {
    self.y = bottom - self.height;
}
// 与其他控件的大小关系
// 影响其大小
- (UIView *)widthEqualTo:(UIView *)brotherView
{
    CGRect brect = brotherView.frame;
    CGRect srect = self.frame;
    srect.size.width = brect.size.width;
    self.frame = srect;
    return self;
}
- (UIView *)heigthEqualTo:(UIView *)brotherView
{
    CGRect brect = brotherView.frame;
    CGRect srect = self.frame;
    srect.size.height = brect.size.height;
    self.frame = srect;
    return self;
}
- (UIView *)sizeEqualTo:(UIView *)brotherView
{
    CGRect brect = brotherView.frame;
    CGRect srect = self.frame;
    srect.size = brect.size;
    self.frame = srect;
    return self;
}

- (UIView *)sizeWith:(CGSize)size
{
    CGRect rect = CGRectZero;
    rect.size = size;
    self.frame = rect;
    return self;
}
// 与父控件对齐的关系
// 只影响其坐标位置，不影响其大小
- (UIView *)alignParentTop
{
    return [self alignParentLeftWithMargin:0];
}
- (UIView *)alignParentBottom
{
    return [self alignParentBottomWithMargin:0];
}
- (UIView *)alignParentLeft
{
    return [self alignParentLeftWithMargin:0];
}
- (UIView *)alignParentRight
{
    return [self alignParentRightWithMargin:0];
}

- (UIView *)alignParentCenter
{
    return [self alignParentCenter:CGPointMake(0, 0)];
}

- (UIView *)alignParentCenter:(CGPoint)margin
{
    CGRect sbounds = self.superview.bounds;
    CGPoint center = CGPointMake(CGRectGetMidX(sbounds), CGRectGetMidY(sbounds));
    center.x += margin.x;
    center.y += margin.y;
    self.center = center;
    return self;
}


- (UIView *)alignParentTopWithMargin:(CGFloat)margin
{
    CGRect rect = self.frame;
    rect.origin.y = self.superview.bounds.origin.y + margin;
    self.frame = rect;
    return self;
}
- (UIView *)alignParentBottomWithMargin:(CGFloat)margin
{
    CGRect superBounds = self.superview.bounds;
    CGRect rect = self.frame;
    rect.origin.y = superBounds.origin.y + superBounds.size.height - margin - rect.size.height;
    self.frame = rect;
    return self;
}
- (UIView *)alignParentLeftWithMargin:(CGFloat)margin
{
    CGRect rect = self.frame;
    rect.origin.x = self.superview.bounds.origin.x + margin;;
    self.frame = rect;
    return self;
}
- (UIView *)alignParentRightWithMargin:(CGFloat)margin
{
    CGRect superBounds = self.superview.bounds;
    CGRect rect = self.frame;
    rect.origin.x = superBounds.origin.x + superBounds.size.width - margin - rect.size.width;
    self.frame = rect;
    return self;
}

- (UIView *)sameWith:(UIView *)brotherView
{
    self.frame = brotherView.frame;
    return self;
}
- (UIView *)layoutToLeftOf:(UIView *)brotherView margin:(CGFloat)margin
{
    CGRect rect = self.frame;
    CGRect brect = brotherView.frame;
    rect.origin.x = brect.origin.x - margin - rect.size.width;
    self.frame = rect;
    return self;
}
- (UIView *)layoutToRightOf:(UIView *)brotherView margin:(CGFloat)margin
{
    CGRect rect = self.frame;
    CGRect brect = brotherView.frame;
    rect.origin.x = brect.origin.x + brect.size.width + margin;
    self.frame = rect;
    return self;
}
- (UIView *)scaleToLeftOf:(UIView *)brotherView margin:(CGFloat)margin
{
    
    CGRect rect = self.frame;
    CGRect brect = brotherView.frame;
    
    if (rect.origin.x < brect.origin.x)
    {
        rect.size.width = brect.origin.x - margin - rect.origin.x;
    }
    else
    {
        rect.size.width = rect.origin.x + rect.size.width - (margin + brect.origin.x);
        rect.origin.x = brect.origin.x + margin;
    }
    
    self.frame = rect;
    return self;
}


// 与父控件的边距
// 只影响其坐标位置，不影响其大小
- (UIView *)marginParetnTop:(CGFloat)top bottom:(CGFloat)bottom left:(CGFloat)left rigth:(CGFloat)right
{
    CGRect superBounds = self.superview.bounds;
    CGRect rect = CGRectZero;
    rect.origin.y = top;
    rect.size.height = superBounds.origin.y + superBounds.size.height - rect.origin.y - bottom;
    
    rect.origin.x = left;
    rect.size.width = superBounds.origin.x + superBounds.size.width - rect.origin.x - right;
    
    self.frame = rect;
    return self;
}

- (UIView *)marginParentWithEdgeInsets:(UIEdgeInsets)inset
{
    return [self marginParetnTop:inset.top bottom:inset.bottom left:inset.left rigth:inset.right];
}

- (UIView *)marginParetn:(CGFloat)margin
{
    self.frame = CGRectInset(self.superview.bounds, margin, margin);
    return self;
}
- (UIView *)marginParetnHorizontal:(CGFloat)margin
{
    CGRect supRect = self.superview.bounds;
    CGRect rect = self.frame;
    rect.origin.x = supRect.origin.x + margin;
    rect.size.width = supRect.size.width - 2 * margin;
    self.frame = rect;
    return self;
}
- (UIView *)marginParetnVertical:(CGFloat)margin
{
    CGRect supRect = self.superview.bounds;
    CGRect rect = self.frame;
    rect.origin.y = supRect.origin.y + margin;
    rect.size.height = supRect.size.height - 2 * margin;
    self.frame = rect;
    return self;
}

- (UIView *)marginParentTop:(CGFloat)margin
{
    CGRect supRect = self.superview.bounds;
    CGRect rect = self.frame;
    rect.origin.y = supRect.origin.y + margin;
    self.frame = rect;
    return self;
}
- (UIView *)marginParentBottom:(CGFloat)margin
{
    CGRect supRect = self.superview.bounds;
    CGRect rect = self.frame;
    rect.size.height = supRect.origin.y + supRect.size.height - margin - rect.origin.y;
    self.frame = rect;
    return self;
}
- (UIView *)marginParentLeft:(CGFloat)margin
{
    CGRect supRect = self.superview.bounds;
    CGRect rect = self.frame;
    rect.origin.x = supRect.origin.x + margin;
    self.frame = rect;
    return self;
}
- (UIView *)marginParentRight:(CGFloat)margin
{
    CGRect supRect = self.superview.bounds;
    CGRect rect = self.frame;
    rect.size.width = supRect.origin.x + supRect.size.width - margin - rect.origin.x;
    self.frame = rect;
    return self;
}

+ (instancetype)viewFromXib
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];
}

- (UIViewController *)viewController {
    UIView* superView = self;
    while (superView)
    {
        UIResponder *nextResponder = [superView nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
            return nextResponder;
        else
            superView = superView.superview;
    }
    return nil;
}


- (UIVisualEffectView *)addBlurEffect {
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.frame = self.bounds;
    [self addSubview:effectview];
    return effectview;
}

- (UIVisualEffectView *)insertBlurEffect {
    return [self insertBlurEffectAtIndex:0];
}

- (UIVisualEffectView *)insertBlurEffectAtIndex:(NSInteger)index {
    return [self insertBlurEffectAtIndex:index effectStyle:UIBlurEffectStyleDark];
}

/* 在固定位置插入高斯模糊 **/
- (UIVisualEffectView *)insertBlurEffectAtIndex:(NSInteger)index effectStyle:(UIBlurEffectStyle)effectStyle {
    return [self insertBlurEffectAtIndex:index effectStyle:effectStyle alpha:1];
}

/* 在固定位置插入高斯模糊 **/
- (UIVisualEffectView *)insertBlurEffectAtIndex:(NSInteger)index effectStyle:(UIBlurEffectStyle)effectStyle alpha:(CGFloat)alpha {
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:effectStyle];
    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.alpha = alpha;
    effectview.frame = self.bounds;
    [self insertSubview:effectview atIndex:index];
    return effectview;
}

- (CGRect)relativePositionTo:(UIView *)aSuperView
{
    BOOL isSubContentOf = [self isSubContentOf:aSuperView];
    while (isSubContentOf)
    {
        return [self relativeTo:aSuperView];
    }
    return CGRectZero;
}

- (BOOL)isSubContentOf:(UIView *)aSuperView
{
    if (self == aSuperView)
    {
        return YES;
    }
    else
    {
        return [self.superview isSubContentOf:aSuperView];
    }
}

- (CGRect)relativeTo:(UIView *)aSuperView
{
    CGPoint originPoint = CGPointZero;
    UIView *view = self;
    while (!(view == aSuperView))
    {
        originPoint.x += view.frame.origin.x;
        originPoint.y += view.frame.origin.y;
        
        if ([view isKindOfClass:[UIScrollView class]])
        {
            originPoint.x -= ((UIScrollView *) view).contentOffset.x;
            originPoint.y -= ((UIScrollView *) view).contentOffset.y;
        }
        
        view = view.superview;
    }
    
    // TODO:如果SuperView是UIWindow,需要结合Transform计算
    return CGRectMake(originPoint.x, originPoint.y, self.frame.size.width, self.frame.size.height);
}
@end
